//
//  ViewController.m
//  26-Masonry
//
//  Created by 曹曹 on 16/8/18.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "ViewController.h"
#import "Masonry.h"

#define kArc4RandomColor [UIColor colorWithRed:arc4random() % 256 / 255.0f green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:0.5];

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 1.使用Masonry, 不需要给初始frame
    UIView *view = [UIView new];
    view.backgroundColor = kArc4RandomColor;
    // 添加约束前必须先把视图添加到父视图上
    [self.view addSubview:view];
    // 设置改视图的约束
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
//        make.left.mas_equalTo(0);
//        make.right.mas_equalTo(0);
//        make.top.mas_equalTo(0);
//        make.bottom.mas_equalTo(0);
    }];
    // 添加蓝色视图, 宽为屏宽一半, 高为屏高1/3, 居中显示
    UIView *blueView = [UIView new];
    blueView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:blueView];
    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.height.mas_equalTo(100);
        make.width.mas_equalTo(view.mas_width).multipliedBy(0.5f);
        make.height.mas_equalTo(view.mas_height).multipliedBy(1 / 3.0f);
        make.center.mas_equalTo(0);
    }];
    // 添加黄色视图,宽高与蓝色形同, centerX 垂直距离10,
    UIView *yellowView = [UIView new];
    yellowView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:yellowView];
    [yellowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.centerX.mas_equalTo(blueView);
        make.top.mas_equalTo(blueView.mas_bottom).mas_offset(10);
    }];
    // yellowView.left = self.view.left * 1.0f + 0;
    [NSLayoutConstraint constraintWithItem:yellowView
                                 attribute:NSLayoutAttributeLeft
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:self.view
                                 attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
